<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2022/05/20
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use ImiApp\ApiServer\Common\AuthApi;
use ImiApp\ApiServer\Api\Controller\Tpmecms;

/**
 * 用户
 * 
 */
class UserBean extends Tpmecms
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];





    /**
     * 支付用户保证金订单
     *
     * @param array $user_caorder 用户保证金订单
     * @param integer $pay_type 支付类型:1=微信,2=支付宝,3=钱包,9=其他
     * @return void
     */
    protected function PayUserSuretyorder($user_suret_order, $pay_type = '3')
    {
        //获取用户保证金支付订单
        $data['order_id'] = $user_suret_order['id'];
        $data['user_id'] = $user_suret_order['user_id'];
        $user = Db::name('user')->where('id','=',$data['user_id'])->find();
        // $data['user_id'] = $user_suret_order['user_id'];
        $order_data = Db::name('pay_order')->whereEx($data)->select()->get();

        if ($order_data) {
            if ($order_data['pay_status'] == '2')
                return $this->error('您已支付保证金');
        } else {
            $order_data['pay_type'] = $pay_type;
            $order_data['money'] = $user_suret_order['money'];
            $order_data['order_id'] = $user_suret_order['id'];
            $order_data['order_status'] = '7'; //7=保证金
            $order_data['pay_status'] = '1'; //支付状态:1=待支付,2=已支付,3=已退款,4=已超时

            $order_data['role_status'] = '1'; //角色:1=用户,2=用户
            $order_data['user_id'] = $user_suret_order['user_id']; //用户id
            $order_data['time'] = time();
            // $order_data['user_id'] = $user_suret_order['user_id'];
            $order_data['id'] = Db::name('pay_order')->insert($order_data)->getLastInsertId();
        }


        switch ($pay_type) {
            case '1':
                return $this->error('支付方式暂未开通');
                break;
            case '2':
                return $this->error('支付方式暂未开通');
                break;
            case '3':
                //判断用户的钱包够不够 
                var_dump($order_data['money'],$user['money'],$user_suret_order);
                if ($order_data['money'] > $user['money']) {
                    return $this->error('用户钱包余额不足', $order_data['money'], CODE_余额不足);
                }
                $this->DecUserMoney($order_data['user_id'], $order_data['money'], '用户支付保证金费用', CODE_用户支付保证金, $order_data['id']);

                $this->UserSuretPaySuccessed($order_data['id'],  $order_data, $order_data['user_id']);
                return $this->success('用户钱包支付成功', $order_data, 3);
                break;
            default:
                return $this->error('没有该支付方式', $order_data);
                break;
        }
    }

    /**
     * 店铺认证支付成功
     *
     * @param string $id
     * @return void
     */
    protected function UserSuretPaySuccessed($id,  $order_data, $user_id)
    {
        //order_status 订单类型:1=现金购物,2=购买积分,3=购买贝壳,4=店铺定金,5=其他
        Db::name('pay_order')->where('id', '=', $id)->update(['pay_status' => '2',  'pay_time' => time(), 'pay_money' => $order_data['money'] * 100]);
        //修改认证订单
        Db::name('user_surety_order')->where('id', '=', $order_data['order_id'])->update(['pay_status' => '2',  'pay_money' => $order_data['money'] * 100]);
        $user = Db::name('user')->where('id', '=', $user_id)->find();
        // var_dump('店铺id' . $user_id, $user['surety'], $order_data['money']);

        $this->IncUserSurety($user_id,  (float) $order_data['money'], '充值保证金', '现金充值', $id, 'surety');
        // Db::name('user')->where('id', '=', $user_id)->setFieldInc('surety', (float) $order_data['money'])->update();
    }
}
